#include "sessiondetailwidget.h"

SessionDetailWidget::SessionDetailWidget(QWidget *parent,const model::UserInfo& userInfo) : QDialog(parent),userInfo(userInfo)
{
    //基本属性设置
    this->setWindowTitle("会话详情");
    this->setWindowIcon(QIcon(":/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setFixedSize(300,300);
    this->setStyleSheet("QWidget {background-color: rgb(255, 255, 255); }");

    //创建网格布局管理器
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(50, 50, 50, 50);
    layout->setSpacing(10);
    layout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    this->setLayout(layout);

    //avataritem 添加组件
    AvatarItem* addUser = new AvatarItem(QIcon(":/image/cross.png"), "添加");
    layout->addWidget(addUser, 0, 0);

#ifdef test
    AvatarItem* currentUser = new AvatarItem(QIcon(":/image/defaultAvatar.png"), "张三");
    layout->addWidget(currentUser, 0, 1);
#endif

    //avataritem 添加对方用户组件
    AvatarItem* currentUser = new AvatarItem(userInfo.userIcon,userInfo.userName);
    layout->addWidget(currentUser, 0, 1);

    QSpacerItem* spaceItem = new QSpacerItem(70, 70);
    layout->addItem(spaceItem, 0, 2);

    //删除按钮
    QPushButton* deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setFixedHeight(50);
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setStyleSheet("QPushButton { border: 1px solid rgb(90,90, 90); border-radius: 5px; } QPushButton:pressed { background-color:rgb(235, 235, 235); }");
    deleteFriendBtn->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    layout->addWidget(deleteFriendBtn, 1, 0, 1, 3);

    /////////////////////////////////////////////////////////////
    /// 信号槽
    /// /////////////////////////////////////////////////////////
    connect(addUser->getAvatarBtn(),&QPushButton::clicked,this,[=](){
        ChooseFriendDialog* dialog = new ChooseFriendDialog(this,userInfo.userId);
        dialog->exec();
    });

    connect(deleteFriendBtn,&QPushButton::clicked,this,&SessionDetailWidget::clickDeleteFriendBtn);
}

void SessionDetailWidget::clickDeleteFriendBtn()
{
    auto result = QMessageBox::warning(this,"删除好友","确认要删除该好友吗",QMessageBox::Ok | QMessageBox::Cancel);
    if(result != QMessageBox::Ok)
    {
        LOG() << "用户取消了删除好友";
        return;
    }

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    dataCenter->deleteFriendAsync(userInfo.userId);
    this->close();
}
